package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.exceptions.ClientExistsException;
import com.acme.bankapp.domain.exceptions.NotEnoughFundsException;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

/**
 * Created by zurdan on 14.11.14.
 * E-mail: Zurdanium@gmail.com
 */
public class BankService {

    public static void modifyBank(final Bank bank) {
        Client[] clients = bank.getClients();
        for (int i = 0; i < bank.getClientsCount(); i++) {
            if (i % 2 == 0) {
                clients[i].getAccount().deposit(100);
            } else {
                try {
                    clients[i].getAccount().withdraw(450);
                } catch (NotEnoughFundsException e) {
                    System.out.println("Error: " + e.getMaxWithdraw() + " max");
                }
            }
        }
        BankDataLoaderService.saveBank(bank);
    }

    public static void addClient(Bank bank, Client client) throws ClientExistsException {
        bank.addClient(client);
    }

    public static void addListener(Bank bank, ClientRegistrationListener listener) {
        bank.addListener(listener);
    }

}
